package net.homeip.jcperera.nu.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.homeip.jcperera.nu.core.boot.Boot;
import net.homeip.jcperera.nu.core.config.CoherenceCache;

/**
 * Servlet implementation class CoherenceCacheServlet
 */
@WebServlet(description = "Coherence Cache Servlet", urlPatterns = { "/cache" })
public class CoherenceCacheServlet extends HttpServlet implements Servlet
{
    private static final long serialVersionUID = 2486879247798084116L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CoherenceCacheServlet()
    {
        super();
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException
    {
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // Output this value to the browser that called this servlet.
        response.setContentType("text/html; charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>SimpleCacheTest</title></head>");
        out.println("<body>");
        out.println("<p><br/>List of pairs property:value from the Coherence Cache</p>");
        out.println("<p>");

        CoherenceCache coherenceCache = Boot.getSystem().getConfiguration().getCoherenceCache();
        Set<String> cacheKeySet = coherenceCache.keySet();
        for (String cacheKey : cacheKeySet)
        {
            String cachedValue = coherenceCache.get(cacheKey).toString();
            out.println(cacheKey + " : " + cachedValue + "<br/>");
        }
        
        out.println("</body></html>");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
     */
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException
    {
        // TODO Auto-generated method stub
    }

}
